{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#自定义函数\" data-toc-modified-id=\"自定义函数-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>自定义函数</a></span></li><li><span><a href=\"#中概互联\" data-toc-modified-id=\"中概互联-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>中概互联</a></span></li><li><span><a href=\"#万科\" data-toc-modified-id=\"万科-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>万科</a></span></li><li><span><a href=\"#相关阅读\" data-toc-modified-id=\"相关阅读-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>相关阅读</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码的详细过程，参见下面的内容：\n",
    "\n",
    "- [用Python来做一个投资计划](https://mp.weixin.qq.com/s/WYuMwCJBrWaBiDs8xp2KMA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 自定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自定义函数，针对单个标的的投资计划\n",
    "\n",
    "def plan(name,price_init,rate_init,shares_init,data):\n",
    "    df = pd.DataFrame(data=data)\n",
    "    df['加仓批次'] = list(range(1,len(df)+1))\n",
    "    df['名称'] = name\n",
    "    df['价格'] = df['价格系数'] * price_init\n",
    "    df['shares'] = ''\n",
    "    df['money'] = ''    \n",
    "\n",
    "    df.loc[df['加仓批次']==1, 'shares'] = shares_init\n",
    "    df.loc[df['加仓批次']==1, 'money'] = df.loc[df['加仓批次']==1, '价格'] * df.loc[df['加仓批次']==1, 'shares']\n",
    "    money_init = df.loc[df['加仓批次']==1, 'money'].values[0]  \n",
    "    for i in range(len(df)-1):\n",
    "        n = i+2   \n",
    "        df.loc[df['加仓批次']==n, 'shares'] = round(money_init*df.loc[df['加仓批次']==n, '加仓比例']/\\\n",
    "                                                (rate_init * df.loc[df['加仓批次']==n, '价格']*100),0)*100\n",
    "        df.loc[df['加仓批次']==n, 'money'] = df.loc[df['加仓批次']==n, '价格'] * df.loc[df['加仓批次']==n, 'shares']\n",
    "    \n",
    "    df['累计仓位'] = df['加仓比例'].cumsum()\n",
    "    df['累计份额'] = df['shares'].cumsum()\n",
    "    df['累计金额'] = df['money'].cumsum()\n",
    "    df['成本'] = df['累计金额']/df['累计份额']\n",
    "    df['下跌幅度'] = 1- df['价格系数']\n",
    "    df['下跌幅度'] = df['下跌幅度'].apply(lambda x:f'下跌{int(round(x*100,0))}%')\n",
    "    df.loc[df['加仓批次']==1, '下跌幅度'] = '首次'\n",
    "    df = df[['名称','加仓批次', '下跌幅度', '价格系数','价格', '加仓比例', '累计仓位', \n",
    "             'shares', 'money','累计份额', '累计金额','成本']]\n",
    "    return df\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 中概互联"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "</style>\n",
       "<table id=\"T_8177e_\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"col_heading level0 col0\" >名称</th>\n",
       "      <th class=\"col_heading level0 col1\" >加仓批次</th>\n",
       "      <th class=\"col_heading level0 col2\" >下跌幅度</th>\n",
       "      <th class=\"col_heading level0 col3\" >价格系数</th>\n",
       "      <th class=\"col_heading level0 col4\" >价格</th>\n",
       "      <th class=\"col_heading level0 col5\" >加仓比例</th>\n",
       "      <th class=\"col_heading level0 col6\" >累计仓位</th>\n",
       "      <th class=\"col_heading level0 col7\" >shares</th>\n",
       "      <th class=\"col_heading level0 col8\" >money</th>\n",
       "      <th class=\"col_heading level0 col9\" >累计份额</th>\n",
       "      <th class=\"col_heading level0 col10\" >累计金额</th>\n",
       "      <th class=\"col_heading level0 col11\" >成本</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row0_col0\" class=\"data row0 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row0_col1\" class=\"data row0 col1\" >1</td>\n",
       "      <td id=\"T_8177e_row0_col2\" class=\"data row0 col2\" >首次</td>\n",
       "      <td id=\"T_8177e_row0_col3\" class=\"data row0 col3\" >100%</td>\n",
       "      <td id=\"T_8177e_row0_col4\" class=\"data row0 col4\" >2.000</td>\n",
       "      <td id=\"T_8177e_row0_col5\" class=\"data row0 col5\" >1.00%</td>\n",
       "      <td id=\"T_8177e_row0_col6\" class=\"data row0 col6\" >1.00%</td>\n",
       "      <td id=\"T_8177e_row0_col7\" class=\"data row0 col7\" >2000</td>\n",
       "      <td id=\"T_8177e_row0_col8\" class=\"data row0 col8\" >￥4000</td>\n",
       "      <td id=\"T_8177e_row0_col9\" class=\"data row0 col9\" >2000</td>\n",
       "      <td id=\"T_8177e_row0_col10\" class=\"data row0 col10\" >￥4000</td>\n",
       "      <td id=\"T_8177e_row0_col11\" class=\"data row0 col11\" >2.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row1_col0\" class=\"data row1 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row1_col1\" class=\"data row1 col1\" >2</td>\n",
       "      <td id=\"T_8177e_row1_col2\" class=\"data row1 col2\" >下跌5%</td>\n",
       "      <td id=\"T_8177e_row1_col3\" class=\"data row1 col3\" >95%</td>\n",
       "      <td id=\"T_8177e_row1_col4\" class=\"data row1 col4\" >1.900</td>\n",
       "      <td id=\"T_8177e_row1_col5\" class=\"data row1 col5\" >2.00%</td>\n",
       "      <td id=\"T_8177e_row1_col6\" class=\"data row1 col6\" >3.00%</td>\n",
       "      <td id=\"T_8177e_row1_col7\" class=\"data row1 col7\" >4200</td>\n",
       "      <td id=\"T_8177e_row1_col8\" class=\"data row1 col8\" >￥7980</td>\n",
       "      <td id=\"T_8177e_row1_col9\" class=\"data row1 col9\" >6200</td>\n",
       "      <td id=\"T_8177e_row1_col10\" class=\"data row1 col10\" >￥11980</td>\n",
       "      <td id=\"T_8177e_row1_col11\" class=\"data row1 col11\" >1.932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row2_col0\" class=\"data row2 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row2_col1\" class=\"data row2 col1\" >3</td>\n",
       "      <td id=\"T_8177e_row2_col2\" class=\"data row2 col2\" >下跌10%</td>\n",
       "      <td id=\"T_8177e_row2_col3\" class=\"data row2 col3\" >90%</td>\n",
       "      <td id=\"T_8177e_row2_col4\" class=\"data row2 col4\" >1.800</td>\n",
       "      <td id=\"T_8177e_row2_col5\" class=\"data row2 col5\" >3.00%</td>\n",
       "      <td id=\"T_8177e_row2_col6\" class=\"data row2 col6\" >6.00%</td>\n",
       "      <td id=\"T_8177e_row2_col7\" class=\"data row2 col7\" >6700</td>\n",
       "      <td id=\"T_8177e_row2_col8\" class=\"data row2 col8\" >￥12060</td>\n",
       "      <td id=\"T_8177e_row2_col9\" class=\"data row2 col9\" >12900</td>\n",
       "      <td id=\"T_8177e_row2_col10\" class=\"data row2 col10\" >￥24040</td>\n",
       "      <td id=\"T_8177e_row2_col11\" class=\"data row2 col11\" >1.864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row3_col0\" class=\"data row3 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row3_col1\" class=\"data row3 col1\" >4</td>\n",
       "      <td id=\"T_8177e_row3_col2\" class=\"data row3 col2\" >下跌15%</td>\n",
       "      <td id=\"T_8177e_row3_col3\" class=\"data row3 col3\" >85%</td>\n",
       "      <td id=\"T_8177e_row3_col4\" class=\"data row3 col4\" >1.700</td>\n",
       "      <td id=\"T_8177e_row3_col5\" class=\"data row3 col5\" >4.00%</td>\n",
       "      <td id=\"T_8177e_row3_col6\" class=\"data row3 col6\" >10.00%</td>\n",
       "      <td id=\"T_8177e_row3_col7\" class=\"data row3 col7\" >9400</td>\n",
       "      <td id=\"T_8177e_row3_col8\" class=\"data row3 col8\" >￥15980</td>\n",
       "      <td id=\"T_8177e_row3_col9\" class=\"data row3 col9\" >22300</td>\n",
       "      <td id=\"T_8177e_row3_col10\" class=\"data row3 col10\" >￥40020</td>\n",
       "      <td id=\"T_8177e_row3_col11\" class=\"data row3 col11\" >1.795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row4_col0\" class=\"data row4 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row4_col1\" class=\"data row4 col1\" >5</td>\n",
       "      <td id=\"T_8177e_row4_col2\" class=\"data row4 col2\" >下跌20%</td>\n",
       "      <td id=\"T_8177e_row4_col3\" class=\"data row4 col3\" >80%</td>\n",
       "      <td id=\"T_8177e_row4_col4\" class=\"data row4 col4\" >1.600</td>\n",
       "      <td id=\"T_8177e_row4_col5\" class=\"data row4 col5\" >5.00%</td>\n",
       "      <td id=\"T_8177e_row4_col6\" class=\"data row4 col6\" >15.00%</td>\n",
       "      <td id=\"T_8177e_row4_col7\" class=\"data row4 col7\" >12500</td>\n",
       "      <td id=\"T_8177e_row4_col8\" class=\"data row4 col8\" >￥20000</td>\n",
       "      <td id=\"T_8177e_row4_col9\" class=\"data row4 col9\" >34800</td>\n",
       "      <td id=\"T_8177e_row4_col10\" class=\"data row4 col10\" >￥60020</td>\n",
       "      <td id=\"T_8177e_row4_col11\" class=\"data row4 col11\" >1.725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row5_col0\" class=\"data row5 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row5_col1\" class=\"data row5 col1\" >6</td>\n",
       "      <td id=\"T_8177e_row5_col2\" class=\"data row5 col2\" >下跌25%</td>\n",
       "      <td id=\"T_8177e_row5_col3\" class=\"data row5 col3\" >75%</td>\n",
       "      <td id=\"T_8177e_row5_col4\" class=\"data row5 col4\" >1.500</td>\n",
       "      <td id=\"T_8177e_row5_col5\" class=\"data row5 col5\" >5.00%</td>\n",
       "      <td id=\"T_8177e_row5_col6\" class=\"data row5 col6\" >20.00%</td>\n",
       "      <td id=\"T_8177e_row5_col7\" class=\"data row5 col7\" >13300</td>\n",
       "      <td id=\"T_8177e_row5_col8\" class=\"data row5 col8\" >￥19950</td>\n",
       "      <td id=\"T_8177e_row5_col9\" class=\"data row5 col9\" >48100</td>\n",
       "      <td id=\"T_8177e_row5_col10\" class=\"data row5 col10\" >￥79970</td>\n",
       "      <td id=\"T_8177e_row5_col11\" class=\"data row5 col11\" >1.663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row6_col0\" class=\"data row6 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row6_col1\" class=\"data row6 col1\" >7</td>\n",
       "      <td id=\"T_8177e_row6_col2\" class=\"data row6 col2\" >下跌30%</td>\n",
       "      <td id=\"T_8177e_row6_col3\" class=\"data row6 col3\" >70%</td>\n",
       "      <td id=\"T_8177e_row6_col4\" class=\"data row6 col4\" >1.400</td>\n",
       "      <td id=\"T_8177e_row6_col5\" class=\"data row6 col5\" >5.00%</td>\n",
       "      <td id=\"T_8177e_row6_col6\" class=\"data row6 col6\" >25.00%</td>\n",
       "      <td id=\"T_8177e_row6_col7\" class=\"data row6 col7\" >14300</td>\n",
       "      <td id=\"T_8177e_row6_col8\" class=\"data row6 col8\" >￥20020</td>\n",
       "      <td id=\"T_8177e_row6_col9\" class=\"data row6 col9\" >62400</td>\n",
       "      <td id=\"T_8177e_row6_col10\" class=\"data row6 col10\" >￥99990</td>\n",
       "      <td id=\"T_8177e_row6_col11\" class=\"data row6 col11\" >1.602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_8177e_row7_col0\" class=\"data row7 col0\" >中概互联-513050</td>\n",
       "      <td id=\"T_8177e_row7_col1\" class=\"data row7 col1\" >8</td>\n",
       "      <td id=\"T_8177e_row7_col2\" class=\"data row7 col2\" >下跌35%</td>\n",
       "      <td id=\"T_8177e_row7_col3\" class=\"data row7 col3\" >65%</td>\n",
       "      <td id=\"T_8177e_row7_col4\" class=\"data row7 col4\" >1.300</td>\n",
       "      <td id=\"T_8177e_row7_col5\" class=\"data row7 col5\" >5.00%</td>\n",
       "      <td id=\"T_8177e_row7_col6\" class=\"data row7 col6\" >30.00%</td>\n",
       "      <td id=\"T_8177e_row7_col7\" class=\"data row7 col7\" >15400</td>\n",
       "      <td id=\"T_8177e_row7_col8\" class=\"data row7 col8\" >￥20020</td>\n",
       "      <td id=\"T_8177e_row7_col9\" class=\"data row7 col9\" >77800</td>\n",
       "      <td id=\"T_8177e_row7_col10\" class=\"data row7 col10\" >￥120010</td>\n",
       "      <td id=\"T_8177e_row7_col11\" class=\"data row7 col11\" >1.543</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fc2b35febb0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标的名称\n",
    "name = '中概互联-513050'\n",
    "\n",
    "# 首次购入价格\n",
    "price_init = 2.0\n",
    "\n",
    "# 首次加仓比例\n",
    "rate_init = 0.01\n",
    "\n",
    "# 首次购入份额数量\n",
    "shares_init = 2000\n",
    "\n",
    "data = {\n",
    "    '价格系数':[1,0.95,0.9,0.85,0.8,0.75,0.7,0.65],\n",
    "    '加仓比例':[rate_init,0.02,0.03,0.04,0.05,0.05,0.05,0.05],\n",
    "    }\n",
    "df = plan(name,price_init,rate_init,shares_init,data)\n",
    "\n",
    "format_dict = {'money': '￥{0:.0f}', \n",
    "               '累计金额': '￥{0:.0f}', \n",
    "               '价格': '{0:.3f}', \n",
    "               '成本': '{0:.3f}', \n",
    "               'shares': '{0:.0f}', \n",
    "               '累计份额': '{0:.0f}', \n",
    "               '价格系数': '{0:.0%}', \n",
    "               '加仓比例': '{0:.2%}', \n",
    "               '累计仓位': '{0:.2%}', \n",
    "                }\n",
    "# print(name)\n",
    "df.style.hide_index().format(format_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 万科"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "</style>\n",
       "<table id=\"T_12f49_\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"col_heading level0 col0\" >名称</th>\n",
       "      <th class=\"col_heading level0 col1\" >加仓批次</th>\n",
       "      <th class=\"col_heading level0 col2\" >下跌幅度</th>\n",
       "      <th class=\"col_heading level0 col3\" >价格系数</th>\n",
       "      <th class=\"col_heading level0 col4\" >价格</th>\n",
       "      <th class=\"col_heading level0 col5\" >加仓比例</th>\n",
       "      <th class=\"col_heading level0 col6\" >累计仓位</th>\n",
       "      <th class=\"col_heading level0 col7\" >shares</th>\n",
       "      <th class=\"col_heading level0 col8\" >money</th>\n",
       "      <th class=\"col_heading level0 col9\" >累计份额</th>\n",
       "      <th class=\"col_heading level0 col10\" >累计金额</th>\n",
       "      <th class=\"col_heading level0 col11\" >成本</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row0_col0\" class=\"data row0 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row0_col1\" class=\"data row0 col1\" >1</td>\n",
       "      <td id=\"T_12f49_row0_col2\" class=\"data row0 col2\" >首次</td>\n",
       "      <td id=\"T_12f49_row0_col3\" class=\"data row0 col3\" >100%</td>\n",
       "      <td id=\"T_12f49_row0_col4\" class=\"data row0 col4\" >22.000</td>\n",
       "      <td id=\"T_12f49_row0_col5\" class=\"data row0 col5\" >1.00%</td>\n",
       "      <td id=\"T_12f49_row0_col6\" class=\"data row0 col6\" >1.00%</td>\n",
       "      <td id=\"T_12f49_row0_col7\" class=\"data row0 col7\" >300</td>\n",
       "      <td id=\"T_12f49_row0_col8\" class=\"data row0 col8\" >￥6600</td>\n",
       "      <td id=\"T_12f49_row0_col9\" class=\"data row0 col9\" >300</td>\n",
       "      <td id=\"T_12f49_row0_col10\" class=\"data row0 col10\" >￥6600</td>\n",
       "      <td id=\"T_12f49_row0_col11\" class=\"data row0 col11\" >22.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row1_col0\" class=\"data row1 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row1_col1\" class=\"data row1 col1\" >2</td>\n",
       "      <td id=\"T_12f49_row1_col2\" class=\"data row1 col2\" >下跌5%</td>\n",
       "      <td id=\"T_12f49_row1_col3\" class=\"data row1 col3\" >95%</td>\n",
       "      <td id=\"T_12f49_row1_col4\" class=\"data row1 col4\" >20.900</td>\n",
       "      <td id=\"T_12f49_row1_col5\" class=\"data row1 col5\" >2.00%</td>\n",
       "      <td id=\"T_12f49_row1_col6\" class=\"data row1 col6\" >3.00%</td>\n",
       "      <td id=\"T_12f49_row1_col7\" class=\"data row1 col7\" >600</td>\n",
       "      <td id=\"T_12f49_row1_col8\" class=\"data row1 col8\" >￥12540</td>\n",
       "      <td id=\"T_12f49_row1_col9\" class=\"data row1 col9\" >900</td>\n",
       "      <td id=\"T_12f49_row1_col10\" class=\"data row1 col10\" >￥19140</td>\n",
       "      <td id=\"T_12f49_row1_col11\" class=\"data row1 col11\" >21.267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row2_col0\" class=\"data row2 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row2_col1\" class=\"data row2 col1\" >3</td>\n",
       "      <td id=\"T_12f49_row2_col2\" class=\"data row2 col2\" >下跌10%</td>\n",
       "      <td id=\"T_12f49_row2_col3\" class=\"data row2 col3\" >90%</td>\n",
       "      <td id=\"T_12f49_row2_col4\" class=\"data row2 col4\" >19.800</td>\n",
       "      <td id=\"T_12f49_row2_col5\" class=\"data row2 col5\" >3.00%</td>\n",
       "      <td id=\"T_12f49_row2_col6\" class=\"data row2 col6\" >6.00%</td>\n",
       "      <td id=\"T_12f49_row2_col7\" class=\"data row2 col7\" >1000</td>\n",
       "      <td id=\"T_12f49_row2_col8\" class=\"data row2 col8\" >￥19800</td>\n",
       "      <td id=\"T_12f49_row2_col9\" class=\"data row2 col9\" >1900</td>\n",
       "      <td id=\"T_12f49_row2_col10\" class=\"data row2 col10\" >￥38940</td>\n",
       "      <td id=\"T_12f49_row2_col11\" class=\"data row2 col11\" >20.495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row3_col0\" class=\"data row3 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row3_col1\" class=\"data row3 col1\" >4</td>\n",
       "      <td id=\"T_12f49_row3_col2\" class=\"data row3 col2\" >下跌15%</td>\n",
       "      <td id=\"T_12f49_row3_col3\" class=\"data row3 col3\" >85%</td>\n",
       "      <td id=\"T_12f49_row3_col4\" class=\"data row3 col4\" >18.700</td>\n",
       "      <td id=\"T_12f49_row3_col5\" class=\"data row3 col5\" >4.00%</td>\n",
       "      <td id=\"T_12f49_row3_col6\" class=\"data row3 col6\" >10.00%</td>\n",
       "      <td id=\"T_12f49_row3_col7\" class=\"data row3 col7\" >1400</td>\n",
       "      <td id=\"T_12f49_row3_col8\" class=\"data row3 col8\" >￥26180</td>\n",
       "      <td id=\"T_12f49_row3_col9\" class=\"data row3 col9\" >3300</td>\n",
       "      <td id=\"T_12f49_row3_col10\" class=\"data row3 col10\" >￥65120</td>\n",
       "      <td id=\"T_12f49_row3_col11\" class=\"data row3 col11\" >19.733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row4_col0\" class=\"data row4 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row4_col1\" class=\"data row4 col1\" >5</td>\n",
       "      <td id=\"T_12f49_row4_col2\" class=\"data row4 col2\" >下跌20%</td>\n",
       "      <td id=\"T_12f49_row4_col3\" class=\"data row4 col3\" >80%</td>\n",
       "      <td id=\"T_12f49_row4_col4\" class=\"data row4 col4\" >17.600</td>\n",
       "      <td id=\"T_12f49_row4_col5\" class=\"data row4 col5\" >5.00%</td>\n",
       "      <td id=\"T_12f49_row4_col6\" class=\"data row4 col6\" >15.00%</td>\n",
       "      <td id=\"T_12f49_row4_col7\" class=\"data row4 col7\" >1900</td>\n",
       "      <td id=\"T_12f49_row4_col8\" class=\"data row4 col8\" >￥33440</td>\n",
       "      <td id=\"T_12f49_row4_col9\" class=\"data row4 col9\" >5200</td>\n",
       "      <td id=\"T_12f49_row4_col10\" class=\"data row4 col10\" >￥98560</td>\n",
       "      <td id=\"T_12f49_row4_col11\" class=\"data row4 col11\" >18.954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row5_col0\" class=\"data row5 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row5_col1\" class=\"data row5 col1\" >6</td>\n",
       "      <td id=\"T_12f49_row5_col2\" class=\"data row5 col2\" >下跌25%</td>\n",
       "      <td id=\"T_12f49_row5_col3\" class=\"data row5 col3\" >75%</td>\n",
       "      <td id=\"T_12f49_row5_col4\" class=\"data row5 col4\" >16.500</td>\n",
       "      <td id=\"T_12f49_row5_col5\" class=\"data row5 col5\" >5.00%</td>\n",
       "      <td id=\"T_12f49_row5_col6\" class=\"data row5 col6\" >20.00%</td>\n",
       "      <td id=\"T_12f49_row5_col7\" class=\"data row5 col7\" >2000</td>\n",
       "      <td id=\"T_12f49_row5_col8\" class=\"data row5 col8\" >￥33000</td>\n",
       "      <td id=\"T_12f49_row5_col9\" class=\"data row5 col9\" >7200</td>\n",
       "      <td id=\"T_12f49_row5_col10\" class=\"data row5 col10\" >￥131560</td>\n",
       "      <td id=\"T_12f49_row5_col11\" class=\"data row5 col11\" >18.272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row6_col0\" class=\"data row6 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row6_col1\" class=\"data row6 col1\" >7</td>\n",
       "      <td id=\"T_12f49_row6_col2\" class=\"data row6 col2\" >下跌30%</td>\n",
       "      <td id=\"T_12f49_row6_col3\" class=\"data row6 col3\" >70%</td>\n",
       "      <td id=\"T_12f49_row6_col4\" class=\"data row6 col4\" >15.400</td>\n",
       "      <td id=\"T_12f49_row6_col5\" class=\"data row6 col5\" >5.00%</td>\n",
       "      <td id=\"T_12f49_row6_col6\" class=\"data row6 col6\" >25.00%</td>\n",
       "      <td id=\"T_12f49_row6_col7\" class=\"data row6 col7\" >2100</td>\n",
       "      <td id=\"T_12f49_row6_col8\" class=\"data row6 col8\" >￥32340</td>\n",
       "      <td id=\"T_12f49_row6_col9\" class=\"data row6 col9\" >9300</td>\n",
       "      <td id=\"T_12f49_row6_col10\" class=\"data row6 col10\" >￥163900</td>\n",
       "      <td id=\"T_12f49_row6_col11\" class=\"data row6 col11\" >17.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_12f49_row7_col0\" class=\"data row7 col0\" >万科</td>\n",
       "      <td id=\"T_12f49_row7_col1\" class=\"data row7 col1\" >8</td>\n",
       "      <td id=\"T_12f49_row7_col2\" class=\"data row7 col2\" >下跌35%</td>\n",
       "      <td id=\"T_12f49_row7_col3\" class=\"data row7 col3\" >65%</td>\n",
       "      <td id=\"T_12f49_row7_col4\" class=\"data row7 col4\" >14.300</td>\n",
       "      <td id=\"T_12f49_row7_col5\" class=\"data row7 col5\" >5.00%</td>\n",
       "      <td id=\"T_12f49_row7_col6\" class=\"data row7 col6\" >30.00%</td>\n",
       "      <td id=\"T_12f49_row7_col7\" class=\"data row7 col7\" >2300</td>\n",
       "      <td id=\"T_12f49_row7_col8\" class=\"data row7 col8\" >￥32890</td>\n",
       "      <td id=\"T_12f49_row7_col9\" class=\"data row7 col9\" >11600</td>\n",
       "      <td id=\"T_12f49_row7_col10\" class=\"data row7 col10\" >￥196790</td>\n",
       "      <td id=\"T_12f49_row7_col11\" class=\"data row7 col11\" >16.965</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fc2b6e7beb0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标的名称\n",
    "name = '万科'\n",
    "\n",
    "# 首次购入价格\n",
    "price_init = 22\n",
    "\n",
    "# 首次加仓比例\n",
    "rate_init = 0.01\n",
    "\n",
    "# 首次购入份额数量\n",
    "shares_init = 300\n",
    "\n",
    "data = {\n",
    "    '价格系数':[1,0.95,0.9,0.85,0.8,0.75,0.7,0.65],\n",
    "    '加仓比例':[rate_init,0.02,0.03,0.04,0.05,0.05,0.05,0.05],\n",
    "    }\n",
    "df = plan(name,price_init,rate_init,shares_init,data)\n",
    "\n",
    "format_dict = {'money': '￥{0:.0f}', \n",
    "               '累计金额': '￥{0:.0f}', \n",
    "               '价格': '{0:.3f}', \n",
    "               '成本': '{0:.3f}', \n",
    "               'shares': '{0:.0f}', \n",
    "               '累计份额': '{0:.0f}', \n",
    "               '价格系数': '{0:.0%}', \n",
    "               '加仓比例': '{0:.2%}', \n",
    "               '累计仓位': '{0:.2%}', \n",
    "                }\n",
    "df.style.hide_index().format(format_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 相关阅读\n",
    "\n",
    "- [视频：Plotly 和 Dash 在投资领域的应用](https://mp.weixin.qq.com/s/VopPrpe_64j5za4AYkmUTg)\n",
    "\n",
    "- [视频：Plotly中绘制股票交易图表](https://mp.weixin.qq.com/s/1TUB2G-xavNm796uXUnRfQ)\n",
    "\n",
    "- [Plotly中绘制三种经典的股票交易图表](https://mp.weixin.qq.com/s/1v3h5-5fNVk7cBdaRKzJkg)\n",
    "\n",
    "- [神器Tushare，财经数据必备工具！](https://mp.weixin.qq.com/s/c1ukemeK12flCgA-lo69fA)\n",
    "\n",
    "- [用 Python 读取巴菲特近期持仓数据](https://mp.weixin.qq.com/s/6h9Xq2lfrZSlRNw_W8aWcg)\n",
    "\n",
    "更多内容，请关注公众号「**Python数据之道**」：\n",
    "\n",
    "![](https://tva1.sinaimg.cn/large/008i3skNgy1gs2d9ca7dvj31hc0u07ng.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
